

import UIKit

@objc protocol MYMyHeaderViewDelegate:NSObjectProtocol {

    func showCountSelectAction(_ indexPath:NSIndexPath);
    func orderSelectAction(_ indexPath:NSIndexPath);
}

class MYMyHeaderView: UITableViewHeaderFooterView,UICollectionViewDelegate,UICollectionViewDataSource {
    var subTextArr = NSArray()
    var model = YQPInfoModel()
    //重写set方法对控件进行赋值操作
//    @objc var model:YQPInfoModel?{
//        didSet{
//
//        }
//    }
    
    @objc weak var delegate:MYMyHeaderViewDelegate?;
    @IBOutlet weak var myOrderLb: UILabel!
    
    @IBOutlet weak var lookupallOrderLb: UILabel!
    @objc @IBOutlet weak var userIcon: UIImageView!
    
    @objc @IBOutlet weak var userName: UILabel!
    
    @objc @IBOutlet weak var vipLb: UILabel!
    
    @objc @IBOutlet weak var integralLb: UILabel!
    
    @objc @IBOutlet weak var userInfoBgView: UIView!
    
    @objc @IBOutlet weak var meOrderBgView: UIView!
    @objc @IBOutlet weak var orderBgView: UIView!
    
    @IBOutlet weak var redBgView: UIView!
    
    lazy var showCountCollView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = (kSwiftkScreenSizeWidth-54-4*50) / 3.0;
        layout.minimumInteritemSpacing = 0.001;
        layout.itemSize = CGSize(width: 50, height: 46);
        let showCountCollView = UICollectionView(frame: CGRect(), collectionViewLayout: layout);
        showCountCollView.backgroundColor = UIColor.clear;
        showCountCollView.dataSource = self;
        showCountCollView.delegate = self;
        
        showCountCollView.register(UINib(nibName: "MYMyHeaderViewCountCell", bundle: nil), forCellWithReuseIdentifier: "MYMyHeaderViewCountCell");
        
        return showCountCollView
    }()
    
    lazy var orderStateCollView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = (kSwiftkScreenSizeWidth-42-5*54) / 4.0;
        layout.minimumInteritemSpacing = 0.001;
        layout.itemSize = CGSize(width: 54, height: 53);
        let orderStateCollView = UICollectionView(frame: CGRect(), collectionViewLayout: layout);
        orderStateCollView.backgroundColor = UIColor.clear;
        orderStateCollView.dataSource = self;
        orderStateCollView.delegate = self;
        
        orderStateCollView.register(UINib(nibName: "MYMyHeaderViewOrderStateCell", bundle: nil), forCellWithReuseIdentifier: "MYMyHeaderViewOrderStateCell")

        return orderStateCollView;
    }()
    
    let textArr = [kYQLSSwift("关注"),kYQLSSwift("粉丝"),kYQLSSwift("评论"),kYQLSSwift("收藏")];

    let orderStateIconArr = ["my_order_state_wait_icon","my_order_state_shipments_icon","my_order_state_waitShipments_icon","my_order_state_comment_icon","my_order_state_refund_icon"];
    let orderStateTextArr = [kYQLSSwift("待付款"),kYQLSSwift("待发货"),kYQLSSwift("待收货"),kYQLSSwift("评价"),kYQLSSwift("售后/退款")];
    
    override func awakeFromNib() {
        super.awakeFromNib();

        
        self.myOrderLb.text = kYQLSSwift("我的订单");
        self.lookupallOrderLb.text = kYQLSSwift("查看全部订单");
//        self.redBgView.backgroundColor = UIColorFromHex(rgbValue: 0x79C4C7);
        
        self.orderBgView.layer.cornerRadius = 12;
        self.orderBgView.clipsToBounds = true;
        
        self.userIcon.layer.cornerRadius = 30;
        self.userIcon.clipsToBounds = true;
        
        self.vipLb.layer.cornerRadius = 10.0;
        self.vipLb.clipsToBounds = true;
        
        self.integralLb.layer.cornerRadius = 10.0;
        self.integralLb.clipsToBounds = true;
        self.redBgView.backgroundColor = appThemeColor
        self.redBgView.addSubview(self.showCountCollView);
        self.showCountCollView.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(-27);
            make?.left.mas_equalTo()(27);
            make?.top.mas_equalTo()(self.userInfoBgView.mas_bottom)?.mas_equalTo()(28);
            make?.bottom.mas_equalTo()(self.orderBgView.mas_top)?.mas_equalTo()(-12);
        }
        
        self.orderBgView.addSubview(self.orderStateCollView);
        self.orderStateCollView.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(self.meOrderBgView.mas_bottom)?.mas_equalTo()(12);
            make?.left.mas_equalTo()(9);
            make?.right.mas_equalTo()(-9);
            make?.bottom.mas_equalTo()(-26);
        };
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews();
        
    }
    //重新对model进行赋值给全局变量，加载数据
    @objc func setModel(_ model:YQPInfoModel){
        self.model = model
        let subTextArr = [kYQLSSwift(model.loveNumber),kYQLSSwift(model.fansNumber),kYQLSSwift(model.commentNumber),kYQLSSwift(model.collectNumber)];
        self.subTextArr = subTextArr as NSArray;
        self.showCountCollView.reloadData()
        
    }
}


extension MYMyHeaderView {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView === self.showCountCollView {
            return self.textArr.count;
        }else{
            return self.orderStateIconArr.count;
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView === self.showCountCollView {
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYMyHeaderViewCountCell", for: indexPath) as? MYMyHeaderViewCountCell else { return UICollectionViewCell() };
            cell.textLb.text = self.textArr[indexPath.item];
            cell.countLb.text = self.subTextArr[indexPath.row] as? String;
            return cell;
        }else{
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYMyHeaderViewOrderStateCell", for: indexPath) as? MYMyHeaderViewOrderStateCell else { return UICollectionViewCell() };
            cell.icon.image = UIImage(named: self.orderStateIconArr[indexPath.item]);
            cell.titleLb.text = self.orderStateTextArr[indexPath.item];
            return cell;
        }

    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView === self.showCountCollView {
            
            if let delegate = self.delegate, delegate.responds(to: Selector(("showCountSelectAction:"))) {
                self.delegate?.showCountSelectAction(indexPath as NSIndexPath);
            }

        }else{
            if let delegate = self.delegate, delegate.responds(to: Selector(("orderSelectAction:"))) {
                self.delegate?.orderSelectAction(indexPath as NSIndexPath);
            }
        }
    }
}
